﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.ViewModels/RecipeSetttingViewModel
* 类       名: RecipeSetttingViewModel
* 功      能:  TD
* 时      间:  2023/9/23 16:22:44
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.Common;
using SiliconSelect.DAL.DataEntity;
using SiliconSelect.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SiliconSelect.ViewModels
{
    public class RecipeSetttingViewModel : NotifyBase
    {

        public RecipeSetttingViewModel()
        {
            ImportRecipeDir = GlobalValues.RecipeImportDir; // 导入目录
            ImportRecipeFile = GlobalValues.RecipeImportFile; // 导入文件
            LoadRecipe();
        }

        /// <summary>
        /// 加载工单数据.
        /// </summary>
        internal void LoadRecipe()
        {
            // 先去获导入的工单路径
            try
            {
                string importPath = Path.Combine(ImportRecipeDir!, ImportRecipeFile!);
                if (!File.Exists(importPath))
                {
                    MessageBox.Show($"配方文件不存在,请重新导入配方文件", "配方导入失败提示框", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                // 加载文件,然后显示内容.
                DataResult<List<List<string>>> ruleContentResult = LocalDataAccess.GetRecipeDectectedRuleContent(importPath);
                //File.SetAttributes(importPath, FileAttributes.Normal);


                if (!ruleContentResult.State)
                {
                    LogHelper.WriteLog(LogType.Fatal, ruleContentResult.State.ToString());
                    MessageBox.Show($"配方加载失败: {ruleContentResult.Message}", "配方加载失败提示框", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                var ruleContentData = ruleContentResult.Data;
                DetectedRuleList.Clear(); // 先清空,防止重新加载的时候数据出现
                foreach (var ruleContent in ruleContentData!)
                {
                    int dataLen = ruleContent.Count;
                    var modelObj = new RecipeDectectedRuleModel()
                    {
                        DetectedName = 0 < dataLen ? ruleContent[0].Trim('\"') : "",
                        Description = 1 < dataLen ? ruleContent[1].Trim('\"') : "",
                        Unit = 2 < dataLen ? ruleContent[2].Trim('\"') : "",
                        Precis = 3 < dataLen ? ruleContent[3].Trim('\"') : "",
                        Level01Val = 4 < dataLen ? ruleContent[4].Trim('\"') : "",
                        Level02Val = 5 < dataLen ? ruleContent[5].Trim('\"') : "",
                        Level03Val = 6 < dataLen ? ruleContent[6].Trim('\"') : "",
                        Level04Val = 7 < dataLen ? ruleContent[7].Trim('\"') : "",
                        Level05Val = 8 < dataLen ? ruleContent[8].Trim('\"') : "",
                        Level06Val = 9 < dataLen ? ruleContent[9].Trim('\"') : "",
                        Level07Val = 10 < dataLen ? ruleContent[10].Trim('\"') : "",
                        Level08Val = 11 < dataLen ? ruleContent[11].Trim('\"') : "",
                        Level09Val = 12 < dataLen ? ruleContent[12].Trim('\"') : "",
                        Level10Val = 13 < dataLen ? ruleContent[13].Trim('\"') : "",
                        Level11Val = 14 < dataLen ? ruleContent[14].Trim('\"') : "",
                        Level12Val = 15 < dataLen ? ruleContent[15].Trim('\"') : "",
                        Level13Val = 16 < dataLen ? ruleContent[16].Trim('\"') : "",
                        Level14Val = 17 < dataLen ? ruleContent[17].Trim('\"') : "",
                        Level15Val = 18 < dataLen ? ruleContent[18].Trim('\"') : "",
                        Level16Val = 19 < dataLen ? ruleContent[19].Trim('\"') : "",
                        Level17Val = 20 < dataLen ? ruleContent[20].Trim('\"') : "",
                        Level18Val = 21 < dataLen ? ruleContent[21].Trim('\"') : "",
                        Level19Val = 22 < dataLen ? ruleContent[22].Trim('\"') : "",
                        Level20Val = 23 < dataLen ? ruleContent[23].Trim('\"') : "",
                        Level21Val = 24 < dataLen ? ruleContent[24].Trim('\"') : "",
                    };
                    DetectedRuleList.Add(modelObj);
                }

                DataResult<List<DetectedLevelEntity>> recipeDetectedLevelResult = LocalDataAccess.GetRecipeDetectedLevel(importPath);
                if (!recipeDetectedLevelResult.State)
                {
                    LogHelper.WriteLog(LogType.Fatal, recipeDetectedLevelResult.Message);
                    MessageBox.Show(recipeDetectedLevelResult.Message);
                }
                // 这里将数据显示到对应的ViewModel上去.
                // 首先是筛选首页上的. 遍历这个列表,然后组装首页要显示的列表.
                if (recipeDetectedLevelResult.State && recipeDetectedLevelResult.Data != null && recipeDetectedLevelResult.Data.Count > 0)
                {
                    RecipeConfigList.Clear();
                    foreach (var item in recipeDetectedLevelResult.Data!)
                    {
                        var temp = new RecipeConfigModel();
                        temp.Level = item.Level;
                        temp.IsUsed = item.IsUsed ? "TRUE" : "FALSE";
                        temp.LevelType = item.LevelType;
                        temp.TypeDescription = item.TypeDescription;
                        temp.BoxesInto = item.BoxesInto;
                        RecipeConfigList.Add(temp);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogType.Exception, ExceptionHelper.GetFullException(ex, MethodBase.GetCurrentMethod()!));
                MessageBox.Show($"导入文件出错: {ex.Message}", "文件导入出错提示框", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


        private ObservableCollection<RecipeConfigModel> _recipeConfigList = new ObservableCollection<RecipeConfigModel>();

        public ObservableCollection<RecipeConfigModel> RecipeConfigList
        {
            get { return _recipeConfigList; }
            set { SetProperty(ref _recipeConfigList, value); }
        }


        /// <summary>
        /// 配方规则数据源
        /// </summary>
        private ObservableCollection<RecipeDectectedRuleModel> _detectedRuleList = new ObservableCollection<RecipeDectectedRuleModel>();

        public ObservableCollection<RecipeDectectedRuleModel> DetectedRuleList
        {
            get { return _detectedRuleList; }
            set { SetProperty(ref _detectedRuleList, value); }
        }



        /// <summary>
        /// 导入的工单的路径,这里可能是用户导入路径,也可能是已有工单路径.
        /// </summary>
        private string? _importRecipeDir;

        public string? ImportRecipeDir
        {
            get { return _importRecipeDir; }
            set { SetProperty(ref _importRecipeDir, value); }
        }

        /// <summary>
        /// 配方导入
        /// </summary>
        private string? _importRecipeFile;

        public string? ImportRecipeFile
        {
            get { return _importRecipeFile; }
            set { SetProperty(ref _importRecipeFile, value); }
        }
    }
}
